from datetime import datetime
import uuid
from werkzeug.security import generate_password_hash, check_password_hash
from app import db

class User(db.Model):
    """用户表模型"""
    __tablename__ = 'users'
    
    id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    username = db.Column(db.String(80), unique=True, nullable=False, index=True)
    email = db.Column(db.String(120), unique=True, nullable=False, index=True)
    password_hash = db.Column(db.String(256), nullable=False)
    role = db.Column(db.String(20), default='user')  # admin, user, guest
    organization = db.Column(db.String(100), nullable=True)
    department = db.Column(db.String(100), nullable=True)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    last_login_at = db.Column(db.DateTime, nullable=True)
    is_active = db.Column(db.Boolean, default=True)
    
    # 关系
    scans = db.relationship('Scan', backref='user', lazy=True)
    target_ranges = db.relationship('TargetRange', backref='user', lazy=True)
    schedules = db.relationship('Schedule', backref='user', lazy=True)
    
    def set_password(self, password):
        """设置用户密码"""
        self.password_hash = generate_password_hash(password)
    
    def check_password(self, password):
        """验证用户密码"""
        return check_password_hash(self.password_hash, password)
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'username': self.username,
            'email': self.email,
            'role': self.role,
            'organization': self.organization,
            'department': self.department,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'last_login_at': self.last_login_at.isoformat() if self.last_login_at else None,
            'is_active': self.is_active
        }

class UserPreference(db.Model):
    """用户首选项表"""
    __tablename__ = 'user_preferences'
    
    id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    user_id = db.Column(db.String(36), db.ForeignKey('users.id'), nullable=False)
    theme = db.Column(db.String(20), default='light')  # light, dark
    language = db.Column(db.String(10), default='zh-CN')
    default_scan_depth = db.Column(db.Integer, default=2)
    default_report_format = db.Column(db.String(20), default='pdf')
    notifications_enabled = db.Column(db.Boolean, default=True)
    email_alerts_enabled = db.Column(db.Boolean, default=True)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'user_id': self.user_id,
            'theme': self.theme,
            'language': self.language,
            'default_scan_depth': self.default_scan_depth,
            'default_report_format': self.default_report_format,
            'notifications_enabled': self.notifications_enabled,
            'email_alerts_enabled': self.email_alerts_enabled,
            'updated_at': self.updated_at.isoformat()
        }